const app = getApp()
const system = app.getAppSysInfo()
var request = require('../../utils/request.js');
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var amapInstance;
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
Page({
  data: {
    Height: 0,
    scale: 13,
    latitude: "",
    longitude: "",
    cid:'',
    controls: [{
      id: 1,
      iconPath: '../../image/reduce.png',
      position: { 
        left: 320,
        top: 100,
        width: 30,
        height: 30
      },
      clickable: true
    }, 
    {
      id: 2,
      iconPath: '../../image/add.png',
      position: {
        left: 320,
        top: 60,
        width: 30,
        height: 30
      },
      clickable: true
    },
    {
      id: 3,
      iconPath: '../../image/dingwei.png',
      position: {
        left: 10,
        top: system.windowHeight - 100,
        width: 39,
        height: 49
      },
      clickable: true
    },
    {
      id: 4,
      iconPath: '../../image/fenlei.png',
      position: {
        left: system.windowWidth - 50,
        top: system.windowHeight - 100,
        width: 60,
        height:70
      },
      clickable: true
    },
    {
      id: 5,
      iconPath: '../../image/tongzhi.png',
      position: {
        left: 0,
        top: 0,
        width: system.windowWidth,
        height: 60
      },
      clickable: true
    },
    ],
    circles: []

  },

  onLoad: function (option) {
    var that = this
    console.log(option)
    that.setData({
      cid: option.cid
    });
    // amapInstance = new amapFile.AMapWX({ key: 'cd17f895f7d70ef688f4bf600e067a8e' });
    amapInstance = new QQMapWX({
      key: 'L6FBZ-YQ4CG-URYQY-IH36I-SXRFZ-IEBI5' //此处使用你自己申请的key
    })
    
    wx.getSystemInfo({
      success: function (res) {
        //设置map高度，根据当前设备宽高满屏显示
        that.setData({
          view: {
            Height: res.windowHeight
          }
        })
      }
    })


    wx.getLocation({
      type: 'gcj02', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: function (res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          lat: res.latitude,
          lon: res.longitude
        })
      }
    })

    console.log(that.data.latitude, that.data.longitude)
  
    // 获取地图麻点
    that.getData();    


  },

  getData: function () {
    var that = this;
    var url = request.dataUrl + '/Orchard/lists';


    if (that.data.cid == '' || that.data.cid == undefined || that.data.cid == null){
      // that.data.cid = 0
      var ucid = 0
      that.setData({
        cid:ucid
      })
    }

    var params = {
      cid: that.data.cid
    }
    request.requestLoading(url, params, '正在加载数据', function (res) {
      console.log(res)
      var list = res.data
      var result = [];
      var icon = ''
      //数据组装
      list.forEach(function (item, index) {
        // if (item.fru_id == 1) {
        //   icon = '../../image/baixiang.png'
        // } else if (item.fru_id == 2) {
        //   icon = '../../image/caomei.png'
        // } else if (item.fru_id == 3) {
        //   icon = '../../image/tongzhi1.png'
        // }
        //为零时显示最近的气泡
        result.push({
          width: 35,
          height: 35,
          iconPath: '../../image/' + item.fru_id+'.png',
          id: item.id,
          category_id: item.category_id,
          latitude: item.latitude,
          longitude: item.longitude,
          title: item.name,
        })
      })
      //赋值
      that.setData({
        markers: result
      });

    }, function (res) { 
      wx.showToast({
        title: '加载数据失败',
        image: '../../image/fail.png'
      })
    })

  },
  
  markertap: function (e) {
    var that = this
    var id = e.markerId;
    var lat = that.data.latitude
    var lon = that.data.longitude
    wx.navigateTo({
      url: '../detail/detail?id=' + id + '&lat=' + lat + '&lon=' + lon
    })
  },

  //点击缩放按钮动态请求数据
  controltap(e) {
    var that = this;
    // console.log("scale===" + this.data.scale)
    if (e.controlId === 1) {
      // if (this.data.scale === 13) {
      that.setData({
        scale: --this.data.scale
      })
      // }
    } else if (e.controlId === 2) {
      //  if (this.data.scale !== 13) {
      that.setData({
        scale: ++this.data.scale
      })
      // }
    } else if (e.controlId === 3) {
      wx.getLocation({
        type: 'gcj02',
        success: function (res) {
          console.log(res)
          var latitude = res.latitude
          var longitude = res.longitude
          that.setData({
            latitude: latitude,
            longitude: longitude
          })
        }
      })
    } else if (e.controlId === 4) {
      wx.navigateTo({
        url: '../classify/classify'
      })
    } else if (e.controlId === 5) {
      wx.navigateTo({
        url: '../huodong/huodong'
      })
    }
  },
  onShow:function(){
    var that=this;
    that.getData();
  },
  // 下拉刷新回调接口
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    var that = this
    // 调取数据
    that.getData();
    setTimeout(function () {
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500)
  }
})